package cn.wl.pchart.controller;

import cn.wl.pchart.entity.Dynamic;
import cn.wl.pchart.entity.Dynamics;
import cn.wl.pchart.tools.OperatorCallback;
import cn.wl.pchart.tools.OperatorCore;

import cn.wl.pchart.service.DynamicService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

// 动态控制器类

// 避免跨域拦截
@CrossOrigin
// 控制器
@RestController
@RequestMapping("/dynamic")
public class DynamicController {

    @Autowired
    DynamicService dynamicService;

    // 获取某用户某个time之前的动态
    @RequestMapping(value = "/getDynamic", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getDynamic(@RequestBody String data) {

        /*
        {
        "id": "用户id",
        "time": "上一个时间，为空则自动设置为当前时间"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String time = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取时间
                    time = rawJson.getString("time");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、time");
                    return returnJson.toString();
                }
                try {
                    if (id != null) {
                        if (time == null) {
                            time = "CURRENT_TIMESTAMP()";
                        }
                        if (time.equals("") || time.equals("null")) {
                            time = "CURRENT_TIMESTAMP()";
                        }
                        Dynamics dynamics = dynamicService.getDynamics(id, time);
                        // System.out.println("动态：" + dynamics);
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功获取动态！");
                        returnJson.put("data", dynamics);
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("获取动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取动态失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 获取一条随机动态
    @RequestMapping(value = "/getDynamicRand", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getDynamicRand(@RequestBody String data) {

        /*
            无参数
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                try {
                    Dynamics dynamics = dynamicService.getDynamics();
                    // System.out.println("动态：" + dynamics);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "成功获取动态！");
                    returnJson.put("data", dynamics);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "获取动态失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 通过did获取一条动态
    @RequestMapping(value = "/getDynamicByDid", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getDynamicByDid(@RequestBody String data) {

        /*
        {
        "did": "动态did",
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String did = null;
                try {
                    // 获取id
                    did = rawJson.getString("did");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：did");
                    return returnJson.toString();
                }
                try {
                    Dynamics dynamics = dynamicService.getDynamics(new BigInteger(did));
                    returnJson.put("code", 0);
                    returnJson.put("msg", "成功获取动态！");
                    returnJson.put("data", dynamics);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取动态失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 获取一个用户的动态
    @RequestMapping(value = "/getDynamicById", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getDynamicById(@RequestBody String data) {

        /*
        {
            "id": "用户id"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、time");
                    return returnJson.toString();
                }
                try {
                    if (id != null) {
                        List<Dynamics> dynamics = dynamicService.getDynamicsById(id);
                        returnJson.put("code", 0);
                        returnJson.put("msg", "成功获取动态！");
                        returnJson.put("data", dynamics);
                        return returnJson.toString();
                    } else {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "id不允许为空！");
                        return returnJson.toString();
                    }
                } catch (Exception e) {
                    System.out.println("获取动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取动态失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 通过标签获取动态
    @RequestMapping(value = "/getDynamicByLabel", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject getDynamicByLabel(@RequestBody String data) {

        /*
        {
        "label": "动态标签",
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String label = null;
                try {
                    // 获取id
                    label = rawJson.getString("label");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：label");
                    return returnJson.toString();
                }
                try {
                    Dynamics dynamics = dynamicService.getDynamics(label);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "成功获取动态！");
                    returnJson.put("data", dynamics);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("获取动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "获取动态失败！");
                    return returnJson.toString();
                }

            }
        });
    }

    // 发布动态
    @RequestMapping(value = "/sendDynamic", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject sendDynamic(@RequestBody String data) {

        /*
        {
            "id": "用户id",
            // "did": "动态id", // 这条由MySQL自动生成
            "type": "动态类型：0 = 原创，1 = 转发",
            // "time": "发布动态的时间", // 这条由MySQL自动生成
            "data": "发布动态的文本内容", // 可以为空
            "media": "发布动态的媒体文件，媒体文件url，字符串数组", // 可以为空
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String type = null;
                String data = null;
                String media = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态类型
                    type = rawJson.getString("type");
                    // 获取动态文本内容
                    data = rawJson.getString("data");
                    // 获取动态附加的媒体文件
                    media = rawJson.getString("media");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、type、data、midia");
                    return returnJson.toString();
                }
                try {
                    Dynamic dynamic = new Dynamic();
                    dynamic.setId(id);
                    dynamic.setType(Integer.valueOf(type));
                    dynamic.setData(data);
                    dynamic.setMedia(media);
                    dynamicService.sendDynamic(dynamic);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "发布动态成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("发布动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "发布动态失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 删除动态
    @RequestMapping(value = "/deleteDynamic", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject deleteDynamic(@RequestBody String data) {
        /*
            "did": "动态id"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String did = null;
                try {
                    // 获取did
                    did = rawJson.getString("did");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：did");
                    return returnJson.toString();
                }
                try {
                    dynamicService.deleteDynamic(new BigInteger(did));
                    returnJson.put("code", 0);
                    returnJson.put("msg", "删除动态成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("删除动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "删除动态失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 查找动态（10条）
    @RequestMapping(value = "/searchDynamic", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject searchDynamic(@RequestBody String data) {
        /*
            "keyword": "关键词"
         */
        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String keyword = null;
                try {
                    // 获取did
                    keyword = rawJson.getString("keyword");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：keyword");
                    return returnJson.toString();
                }
                try {
                    List<BigInteger> dids = dynamicService.searchDynamic(keyword);
                    if (dids == null) {
                        returnJson.put("code", 3);
                        returnJson.put("msg", "查询动态did时出错！");
                        return returnJson.toString();
                    }
                    if (dids.size() == 0) {
                        returnJson.put("code", 4);
                        returnJson.put("msg", "没有搜索到相关动态的did！");
                        return returnJson.toString();
                    }

                    List<Dynamics> dynamics = new ArrayList<Dynamics>();

                    for (BigInteger i : dids) {
                        dynamics.add(dynamicService.getDynamics(i));
                    }
                    returnJson.put("code", 0);
                    returnJson.put("msg", "搜索动态成功！");
                    returnJson.put("data", dynamics);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("搜索动态失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "搜索动态失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 点赞
    @RequestMapping(value = "/doLike", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject doLike(@RequestBody String data) {

        /*
        {
            "did": "动态id",
            "id": "用户id"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String did = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态did
                    did = rawJson.getString("did");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、did");
                    return returnJson.toString();
                }
                try {
                    dynamicService.doLike(new BigInteger(did), id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "点赞成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("点赞失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "点赞失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 取消点赞
    @RequestMapping(value = "/cancelLike", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject cancelLike(@RequestBody String data) {

        /*
        {
            "did": "动态id",
            "id": "用户id"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String did = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态did
                    did = rawJson.getString("did");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、did");
                    return returnJson.toString();
                }
                try {
                    dynamicService.cancelLike(new BigInteger(did), id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "取消点赞成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("取消点赞失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "取消点赞失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 评论
    @RequestMapping(value = "/doComment", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject doComment(@RequestBody String data) {

        /*
        {
            "did": "动态id",
            "id": "用户id",
            "data": "评论内容"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String did = null;
                String data = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态did
                    did = rawJson.getString("did");
                    // 获取评论内容
                    data = rawJson.getString("data");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、did、data");
                    return returnJson.toString();
                }
                try {
                    dynamicService.doComment(new BigInteger(did), id, data);
                    Dynamics dynamics = dynamicService.getDynamics(new BigInteger(did));
//                    JSONObject temp = new JSONObject();
//                    temp.put("test", dynamics);
//                    System.out.println(temp);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "评论成功！");
                    returnJson.put("data", dynamics);
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("评论失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "评论失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 删除评论
    @RequestMapping(value = "/deleteComment", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject deleteComment(@RequestBody String data) {

        /*
        {
            "did": "动态id",
            "id": "用户id",
            "data": "评论内容"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String did = null;
                String data = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态did
                    did = rawJson.getString("did");
                    // 获取评论内容
                    data = rawJson.getString("data");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、did、data");
                    return returnJson.toString();
                }
                try {
                    dynamicService.deleteComment(new BigInteger(did), id, data);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "删除评论成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("删除评论失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "删除评论失败！");
                    return returnJson.toString();
                }
            }
        });
    }

    // 转发
    @RequestMapping(value = "/doForward", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public JSONObject doForward(@RequestBody String data) {

        /*
        {
            "did": "动态id",
            "id": "用户id"
        }
        */

        return OperatorCore.handle(data, new OperatorCallback() {
            @Override
            public String call() throws Exception {
                String id = null;
                String did = null;
                try {
                    // 获取id
                    id = rawJson.getString("id");
                    // 获取动态did
                    did = rawJson.getString("did");
                } catch (Exception e) {
                    System.out.println("从JSON对象中获取数据失败！");
                    e.printStackTrace();
                    returnJson.put("code", 1);
                    returnJson.put("msg", "从JSON对象中获取数据失败！请检查数据格式：id、did");
                    return returnJson.toString();
                }
                try {
                    dynamicService.doForward(new BigInteger(did), id);
                    returnJson.put("code", 0);
                    returnJson.put("msg", "转发成功！");
                    return returnJson.toString();
                } catch (Exception e) {
                    System.out.println("转发失败！");
                    e.printStackTrace();
                    returnJson.put("code", 2);
                    returnJson.put("msg", "转发失败！");
                    return returnJson.toString();
                }
            }
        });
    }
}
